package top.rushpeak.edu03.admin.core;


import java.util.List;

import org.quartz.Job;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;

import top.rushpeak.edu03.admin.job.DynamicJob;
import top.rushpeak.edu03.admin.model._MappingKit;
import top.rushpeak.edu03.admin.util.QuartzManager;




public class MyConfig extends JFinalConfig {
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("properties/config.properties");
		me.setMaxPostSize(1024*1024*10);
		me.setReportAfterInvocation(false);
		me.setDevMode(PropKit.getBoolean("devMode", true));
		me.setError404View("/WEB-INF/pages/error/404.html");
		me.setError500View("/WEB-INF/pages/error/500.html");
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		
		me.add(new MyRoutes());
		
//		me.add(new AdminRoutes());

	}
	
	
	public void configEngine(Engine me) {
		//模板頁面引擎
		String basePath = "/WEB-INF/pages/common";
		String[] templates = {
//				"/_admin.html",
				"/_top-nav.html",
				"/_paginate.html"		
		}; 
		for(String tmp: templates){
			me.addSharedFunction(basePath+tmp);
		}
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置Druid数据库连接池插件
		DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
		me.add(druidPlugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setShowSql(PropKit.getBoolean("devMode", true));
		// 所有映射在 MappingKit 中自动化搞定
		_MappingKit.mapping(arp);
		me.add(arp);
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
//		me.add(new LoginInterceptor());
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {

	}

	@Override
	public void afterJFinalStart() {
		System.out.println("afterJFinalStart");
		super.afterJFinalStart();
		//加载定时任务数据库配置
		QuartzManager qm = new QuartzManager();
		qm.initJob();

	}

	@Override
	public void beforeJFinalStop() {
		
		super.beforeJFinalStop();
		//关闭定时任务管理器
		QuartzManager qm = new QuartzManager();
		qm.shutdown();
		
	}
	
	
}
